package com.mayi.zds.service.imp;

import com.mayi.zds.dao.GoodsDAO;
import com.mayi.zds.dao.JudgeDAO;
import com.mayi.zds.dao.LabelDAO;
import com.mayi.zds.entity.Goods;
import com.mayi.zds.entity.Label;
import com.mayi.zds.service.GoodsService;
import com.mayi.zds.utils.ZdsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private GoodsDAO goodsDAO;
    @Resource
    private LabelDao labelDao;
    private JudgeDAO judgeDAO;
    @Override
    public void add(Goods goods, String[] labelNames,String businessId) {
        goods.setId(ZdsUtils.produceId());
    goodsDAO.add(goods);
        for (String labelName : labelNames) {
            if(labelDao.getByName(labelName)!=null){
                Label label = new Label();
                label.setName(labelName);
                label.setId(ZdsUtils.produceId());
                label.setBusinessId(businessId);
                label.setGoodsNum(1);
                labelDao.add(label);

            }else{
                Label label = labelDao.getByName(labelName);
                labelDao.updateGoodsNum(label.getGoodsNum()+1,label.getId());

            }




        }
    }

    /**
     * @description: 删除商品
     * @params:
     * @return:
     * @author: 胡飞
     * @time: 2019/12/27 15:56
     */
    @Override
    public void delete(String goodsId) {
        //删除评价
        judgeDAO.delete(goodsId);
        //删除商品标签中间表的数据
        goodsDAO.deleteGoodsLabel(goodsId);
        //删除商品表中数据
        goodsDAO.delete(goodsId);
    }

    @Override
    public void down(String goodsId) {
        goodsDAO.updateStauts(goodsId,"n");
    }


}
